package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //套餐与菜品:保存一个套餐,多个菜品
        save(setmealDto);
        //遍历一个套餐中的多个菜品,为每一个菜品关联套餐信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            //setmealDto.getId()为setmeal套餐的id,在关系表中设置每一个对应的setmealid
            setmealDish.setSetmealId(setmealDto.getId());
        });
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐，同时需要删除套餐和菜品的关联数据
     *
     * @param ids
     */
    public void removeWithDish(List<Long> ids) {
        //查询套餐状态,确定是否可以删除;起售状态不可删除
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId, ids).eq(Setmeal::getStatus, 1);
        int count = count(lqw);
        if (count > 0) {
            //套餐启售,不可删除
            throw new CustomException("套餐正在售卖,不能删除");
        }
        //套餐禁售,可以删除;先删除setmeal中的数据
        removeByIds(ids);
        //再删除setmealdish关系表中的数据setmeal_id in (ids)
        LambdaQueryWrapper<SetmealDish> lqw2 = new LambdaQueryWrapper<>();
        lqw2.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(lqw2);
    }

    /**
     * 查询当前套餐信息
     *
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //查询当前套餐信息;
        Setmeal setmeal = getById(id);
        //套餐中包含多个菜品,查询setmeal与dish对应数据,封装到dto
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        //select * from setmealdish where setmealdish=id;查询到当前套餐的多个菜品
        lqw.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishService.list(lqw);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        //添加当前套餐的菜品信息
        setmealDto.setSetmealDishes(setmealDishList);
        return setmealDto;
    }

    /**
     * 修改套餐信息
     *
     * @param setmealDto
     */
    @Override
    public void update(SetmealDto setmealDto) {
        //修改当前套餐信息
        updateById(setmealDto);
        //修改关联的菜品信息:删除-重新添加
        //SetmealDish中查询setmeal的id,删除
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(lqw);
        //重新添加:获取dto中的SetmealDishes,设置到当前套餐中
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
        });
        setmealDishService.saveBatch(setmealDishes);
    }

}
